/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOImpl;

import DAO.ICaseRecordDAO;
import Exception.DBConnectionException;
import POJO.Criminal;
import Util.DBUtils;
import java.math.BigDecimal;
import java.sql.*;

/**
 *
 * @author NAT
 */
public class CaseRecordImpl extends BaseImpl implements ICaseRecordDAO{

    @Override
    public boolean insert(Object obj) {
        boolean result = false;
        
        try{
            Connection con =  DBUtils.getConnection();
            
            String query = "INSERT INTO [dbo].[Student](FirstName,LastName)VALUES(?,?)";
            
            PreparedStatement pstmt = con.prepareStatement(query);
            
            Criminal classObj = null;
            if(obj instanceof Criminal  ){
                 classObj = (Criminal)obj;
            }
            if(classObj != null){
                pstmt.setString(1, classObj.getName());
                pstmt.setString(2, classObj.getNickName());
                
            }
            else{
                return false;
            }
            
            //execute
            int effectedRows =  pstmt.executeUpdate();
            
            if(effectedRows > 0){
                result = true;
            }
            else{
                result = false;
            }
            
        }
        catch (SQLException ex) {
        
        }
        catch(DBConnectionException dbException){
            
        }
        
        return result;
    }

    @Override
    public boolean update(Object obj) {
        try{
            Connection con =  DBUtils.getConnection();
            
            String query = "SELECT * FROM Sales.Store WHERE CustomerID LIKE 70";
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = stmt.executeQuery(query);

            //Move to first row for cusor
            rs.first();

            //Update data n-order column in the first row
            rs.updateString("Name", "abc");

            //Commit the row updation
            rs.updateRow();

        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(Object obj) {
        try{
            Connection con =  DBUtils.getConnection();
            
            String query = " SELECT * FROM Student ";
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = stmt.executeQuery(query);

            //Move to first row for cusor
            rs.last();

            //Commit the row deletion
            rs.deleteRow();
            
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }
    
    @Override
    public void showCaseRecord() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
